package com.zggk.lzz.dtfd;


import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.MyLocationStyle;
import com.amap.api.maps.model.PolylineOptions;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.district.DistrictItem;
import com.amap.api.services.district.DistrictResult;
import com.amap.api.services.district.DistrictSearch;
import com.amap.api.services.district.DistrictSearchQuery;
import com.zggk.lzz.R;
import com.zggk.lzz.dtfbevent.DtfbEventActivity;
import com.zggk.lzz.dtfbtunnel.DtfbTunnelActivity;
import com.zggk.lzz.dtfdbridge.DtfdBridgeActivity;
import com.zggk.lzz.mvp.MVPBaseActivity;
import com.zggk.lzz.roadlist.RoadListActivity;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * MVPPlugin
 * 邮箱 784787081@qq.com
 */

public class DtfdActivity extends MVPBaseActivity<DtfdContract.View, DtfdPresenter> implements DtfdContract.View {


    @Bind(R.id.map)
    MapView mMapView;
    @Bind(R.id.back_lay)
    RelativeLayout backLay;
    @Bind(R.id.lx_te)
    ImageView lxTe;
    @Bind(R.id.ql_te)
    ImageView qlTe;
    @Bind(R.id.sd_te)
    ImageView sdTe;
    @Bind(R.id.lxlb1_te)
    TextView lxlb1Te;
    @Bind(R.id.lxlb_te)
    RelativeLayout lxlbTe;
    @Bind(R.id.disease_new_parent_layout)
    RelativeLayout diseaseNewParentLayout;
    private AMap aMap;
    private MyLocationStyle myLocationStyle;

    @Override
    public void onRequestError(String msg) {

    }

    @Override
    protected int getLayout() {
        return R.layout.act_dtfb;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
        //在activity执行onCreate时执行mMapView.onCreate(savedInstanceState)，创建地图
        initMap(savedInstanceState);
        initLan();
        initSearch();
    }
    /**
     * @Description: (高亮省份)
     * @author 张成昆
     * @date 2020-4-28
     */
    private void initSearch() {
        DistrictSearch search = new DistrictSearch(DtfdActivity.this);
        DistrictSearchQuery query = new DistrictSearchQuery();
        query.setKeywords("广东省");//传入关键字
        query.setShowBoundary(true);//是否返回边界值
        search.setQuery(query);
        search.setOnDistrictSearchListener(new DistrictSearch.OnDistrictSearchListener() {
            @Override
            public void onDistrictSearched(DistrictResult districtResult) {
                if (districtResult == null || districtResult.getDistrict() == null) {
                    return;
                }
                final DistrictItem item = districtResult.getDistrict().get(0);
                if (item == null) {
                    return;
                }
                LatLonPoint centerLatLng = item.getCenter();//得到行政中心点坐标
                if (centerLatLng != null) {  //地图加载时就显示行政区域
                    aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(centerLatLng.getLatitude(), centerLatLng.getLongitude()), 8));//13为缩放级别
                }
                new Thread() {
                    private PolylineOptions polylineOption;

                    public void run() {
                        String[] polyStr = item.districtBoundary();
                        if (polyStr == null || polyStr.length == 0) {
                            return;
                        }
                        for (String str : polyStr) {
                            String[] lat = str.split(";");
                            polylineOption = new PolylineOptions();
                            boolean isFirst = true;
                            LatLng firstLatLng = null;
                            for (String latstr : lat) {
                                String[] lats = latstr.split(",");
                                if (isFirst) {
                                    isFirst = false;
                                    firstLatLng = new LatLng(Double
                                            .parseDouble(lats[1]), Double
                                            .parseDouble(lats[0]));
                                }
                                polylineOption.add(new LatLng(Double
                                        .parseDouble(lats[1]), Double
                                        .parseDouble(lats[0])));
                            }
                            if (firstLatLng != null) {
                                polylineOption.add(firstLatLng);
                            }

                            polylineOption.width(7).color(getResources().getColor(R.color.bule_color));
                            Message message = handler.obtainMessage();
                            message.obj = polylineOption;
                            handler.sendMessage(message);
                        }
                    }
                }.start();
            }
        });//绑定监听器
        search.searchDistrictAnsy();//开始搜索
    }

    private Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            PolylineOptions polylineOption = (PolylineOptions) msg.obj;
            aMap.addPolyline(polylineOption);
        }
    };
    private void initLan() {
        myLocationStyle = new MyLocationStyle();//初始化定位蓝点样式类myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE);//连续定位、且将视角移动到地图中心点，定位点依照设备方向旋转，并且会跟随设备移动。（1秒1次定位）如果不设置myLocationType，默认也会执行此种模式。
        myLocationStyle.interval(2000); //设置连续定位模式下的定位间隔，只在连续定位模式下生效，单次定位模式下不会生效。单位为毫秒。
        myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_FOLLOW_NO_CENTER);//只定位一次。
        aMap.setMyLocationStyle(myLocationStyle);//设置定位蓝点的Style
        aMap.setMyLocationEnabled(true);// 设置为true表示启动显示定位蓝点，false表示隐藏定位蓝点并不进行定位，默认是false。
    }

    private void initMap(Bundle savedInstanceState) {
        mMapView.onCreate(savedInstanceState);
        if (aMap == null) {
            aMap = mMapView.getMap();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //在activity执行onDestroy时执行mMapView.onDestroy()，销毁地图
        mMapView.onDestroy();
    }

    @Override
    protected void onResume() {
        super.onResume();
        //在activity执行onResume时执行mMapView.onResume ()，重新绘制加载地图
        mMapView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        //在activity执行onPause时执行mMapView.onPause ()，暂停地图的绘制
        mMapView.onPause();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        //在activity执行onSaveInstanceState时执行mMapView.onSaveInstanceState (outState)，保存地图当前的状态
        mMapView.onSaveInstanceState(outState);
    }

    @OnClick({R.id.ql_te, R.id.sd_te, R.id.lxlb_te, R.id.sj_te, R.id.back_lay})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_lay:
                finish();
                break;
            case R.id.lxlb_te:
                startActivity(new Intent(DtfdActivity.this, RoadListActivity.class));
                break;
            case R.id.ql_te:
                startActivity(new Intent(DtfdActivity.this, DtfdBridgeActivity.class));
                break;
            case R.id.sd_te:
                startActivity(new Intent(DtfdActivity.this, DtfbTunnelActivity.class));
                break;
            case R.id.sj_te:
                startActivity(new Intent(DtfdActivity.this, DtfbEventActivity.class));
                break;
        }
    }
}
